<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>魏远商城</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="css/app.css" />
		<link href="css/main.css" rel="stylesheet" />
		<link href="css/sure.css" rel="stylesheet" />
		<script src="js/mui.min.js"></script>
		<script src="js/jquery-3.1.1.min.js"></script>
		<script src="js/url.js"></script>
	</head>

	<body>
		<div class="head">
			<img class="mui-action-back" src="img/back-le.png" alt="">
			<p>支付</p>
		</div>
		<div class="content">
			<!--<div class="content-d">
            <p>学校贷名校贷</p>
            <div>
            	<img src="../../images/school.png" alt="">
            </div>
            <p>共<span></span>件，合计：<span class="red">¥<span></span></span></p>
        </div>-->
		</div>
		<div class="way">
			<p>支付方式</p>
			<!--<div class="way1">
                <span class="quan"></span>
                <img class="img1" src="img/vip-zhi.png" alt="">
                <span>支付宝</span>
            </div>-->
			<div class="way2">
				<span class="quan"></span>
				<img class="img2" src="img/vip-wei.png" alt="">
				<span>微信</span>
			</div>
			<div class="way3">
				<span class="quan"></span>
				<img class="img3" src="img/yueee.png" alt="">
				<span>余额支付</span>
			</div>
		</div>
		<div class="accound">
			<div class="all-pay">
				<p>共<span class="how"></span>件，合计：<span class="red">¥<span class="allprice"></span></span>
				</p>
			</div>
			<div class="sure-pay">确认支付</div>
		</div>
	</body>
	<script type="text/javascript">
		$('.allprice').text(localStorage.allpriceall);
		$('.how').text(localStorage.alltimes);
		/*选择微信还是支付宝支付*/
		$('.way>div').on('tap', function() {
			$(this).find('.quan').addClass('addred');
			$(this).siblings().find('.quan').removeClass('addred');
		})
		/*点击确认支付判断是微信还是支付宝*/
		$('.sure-pay').on('tap', function() {
			
			//        plus.nativeUI.showWaiting();
			console.log($('.way1>.quan').hasClass('addred'));
			console.log($('.way2>.quan').hasClass('addred'));
			console.log($('.way3>.quan').hasClass('addred'));
			var way1 = $('.way1>.quan').hasClass('addred');
			var way2 = $('.way2>.quan').hasClass('addred');
			var way3 = $('.way3>.quan').hasClass('addred');
			if(way1 || way2 || way3) {
				if($('.way1>.quan').hasClass('addred')) {
				} else if($('.way2>.quan').hasClass('addred')) {
					console.log('微信');
//					mui.toast('目前不支持微信支付');
					pay("weixin");

				} else if($('.way3>.quan').hasClass('addred')) {
					console.log('余额');
					balpay();
					/*href('success.html')*/
				}
			} else {
				mui.toast('请选择支付方式');
				console.log('请选择支付方式')
			}
		})

		function pay(type) {
			console.log(type);
			if(type == "weixin") {
				$.ajax({
					url: ur + '/payment/wxpay/unifiedorder',
					headers: {
						"X-Token": localStorage.token
					},
					type: 'POST',
					dataType: 'json',
					data: {
						'orderVO.id': localStorage.sureorderid,
						'payType': 'jsapi'
					},
					success: function(data) {
						console.log(JSON.stringify(data));
						var data = data.data;
						//调起微信支付js
						onBridgeReady(data);
					},
					error:function(error){
						mui.alert('系统繁忙，请稍后再试')
					}
				})

			}
		}

		//微信支付
		function onBridgeReady(obj) {
			WeixinJSBridge.invoke(
				'getBrandWCPayRequest', {
					"appId": obj.appId, //公众号名称，由商户传入
					"timeStamp": obj.timeStamp, //时间戳，自1970年以来的秒数     
					"nonceStr": obj.nonceStr, //随机串     
					"package": obj.package,
					"signType": obj.signType, //微信签名方式：     
					"paySign": obj.paySign //微信签名 
				},
				function(res) {
					if(res.err_msg == "get_brand_wcpay_request:ok") {
					window.location.href='myorder1.html';
					} else {
						mui.toast('订单未支付')
//						alert(JSON.stringify(res));
						console.log(res.err_msg);
					}
				}
			);
		}
		//		/*余额支付*/
		function balpay() {
			$.ajax({
					url: ur + '/payment/alipay/useBalancePay',
					type: 'POST',
					headers: {
						"X-Token": localStorage.token
					},
					dataType: 'json',
					data: {
						"orderVO.orderNum": localStorage.sureorder,
					},
					success:function(data){
						if(data.result.code == 200) {
							mui.toast('支付成功');
							mui.openWindow({
								url: "paysuccess.html",
							});
						} else if(data.result.code == 201) {
							mui.toast('账户不存在');
						} else if(data.result.code == 202) {
							mui.toast('余额不足');
						} else if(data.result.code == 203) {
							mui.toast('订单不存在');
						} else if(data.result.code == 110) {
							mui.toast('系统繁忙，请稍后重试');
						}
					},
					error:function(e){
						mui.alert('系统繁忙，请稍后再试')
					}
				})
		}
	</script>

</html>